package com.hcyy.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hcyy.dao.AttrTypeDao;
import com.hcyy.dao.AttributeDao;
import com.hcyy.dao.SpItemDao;
import com.hcyy.entity.AttrType;
import com.hcyy.entity.SpItem;

@Controller
@RequestMapping("/NonDrug")
public class NonDrugController {
	
	@Autowired
	private SpItemDao spItemDao;
	@Autowired
	private AttrTypeDao attrTypeDao;
	@Autowired
	private AttributeDao attributeDao;
	
	@RequestMapping("/Detail")
	public ModelAndView toDetail(HttpSession session,String productid){
		ModelAndView mv = new ModelAndView();
		SpItem spItem = spItemDao.getSpItemDetail(productid);
		List<AttrType> attrtypeList = attrTypeDao.findAttrTypeList(productid);
		mv.addObject("spItem",spItem);		
		mv.addObject("attrtypeList",attrtypeList);
		mv.setViewName("nondrug/ProductDetail");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/MatchAttr")
	public List<String> MatchAttr(String attrid, String spid){
		String attributeId=null;
		String attrid1=attrid;
		String attrid2=null;
		if(attrid.indexOf(",")>0){
			String[] attrids=attrid.split(",");
			attrid1=attrids[0];
			attrid2=attrids[1];
		}
		attributeId=attributeDao.findMatchAttr(attrid1, attrid2, spid);
		String[] ids=attributeId.split(",");
		List<String> idArray=new ArrayList<String>();
		for(String attid:ids){
			if(!attid.equals(attrid+"")&&!idArray.contains(attid)){
				idArray.add(attid);
			}
		}
		return idArray;
	}
}
